/*
 * Course:    CS 590 Web Services
 * Semseter:  Spring 2009
 * Professor: Sedlemeyer
 * Project:   CHEAT (Code Help Educational Assignment Tool)
 * Due Date:  May 1, 2009
 * Team:      Sarcasm
 *            (Baker, Jason; Mitchell, Robin; Shie, Keith)
 */

package cheat.entities;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Define the type of access a {@linkplain RegisteredUser} has within the system.
 *
 * <p>There are three levels:<ul>
 * <li>Admin - Can grant/deny users & delete assignments, download full assignments, query</li>
 * <li>Faculty - Can submit/replace assignments, download full assignments, query</li>
 * <li>Student - Can download student portion of assignments, query</li>
 * </ul></p>
 *
 * @author Keith Shie
 */
@Entity
@Table(name = "accesstype")
@NamedQueries({@NamedQuery(name = "AccessType.findAll", query = "SELECT a FROM AccessType a"),
        @NamedQuery(name = "AccessType.findByAccessCode", query = "SELECT a FROM AccessType a WHERE a.accessCode = :accessCode"),
        @NamedQuery(name = "AccessType.findByDescription", query =
        "SELECT a FROM AccessType a WHERE a.description = :description")})
public class AccessType implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @Basic(optional = false)
  @Column(name = "accesscd", nullable = false, length = 8)
  private String accessCode;
  @Basic(optional = false)
  @Column(name = "description", nullable = false, length = 30)
  private String description;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "accessCode")
  private Collection<RegisteredUser> reguserCollection;

  /**
   * String representing the ADMIN level
   */
  final static public String ADMIN_CODE = "ADMIN";
  /**
   * AccessType Object representing the ADMIN level
   */
  final static public AccessType ADMIN_TYPE = new AccessType(ADMIN_CODE);

  /**
   * String representing the FACULTY level
   */
  final static public String FACULTY_CODE = "INST";
  /**
   * AccessType Object representing the FACULTY level
   */
  final static public AccessType FACULTY_TYPE = new AccessType(FACULTY_CODE);

  /**
   * String representing the STUDENT level
   */
  final static public String STUDENT_CODE = "STDNT";
  /**
   * AccessType Object representing the STUDENT level
   */
  final static public AccessType STUDENT_TYPE = new AccessType(STUDENT_CODE);

  public AccessType() {
  }

  public AccessType(String accesscd) {
    this.accessCode = accesscd;
  }

  public AccessType(String accesscd, String description) {
    this.accessCode = accesscd;
    this.description = description;
  }

  public String getAccessCode() {
    return accessCode;
  }

  public void setAccessCode(String accessCode) {
    this.accessCode = accessCode;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public Collection<RegisteredUser> getReguserCollection() {
    return reguserCollection;
  }

  public void setReguserCollection(Collection<RegisteredUser> reguserCollection) {
    this.reguserCollection = reguserCollection;
  }

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (accessCode != null ? accessCode.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof AccessType)) {
      return false;
    }
    AccessType other = (AccessType) object;
    if ((this.accessCode == null && other.accessCode != null) || (this.accessCode != null && !this.accessCode.equals(other.accessCode))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "cheat.entities.AccessType[accessCode=" + accessCode + "]";
  }

}
